package leetcode.editor.cn;

//Java：路径总和
public class PathSum {
    public static void main(String[] args) {
        Solution solution = new PathSum().new Solution();
        // TO TEST
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        public int target;

        public boolean hasPathSum(TreeNode root, int targetSum) {
            target = targetSum;
            return deal(root, 0);
        }

        private boolean deal(TreeNode root, int s) {
            if (root == null) {
                return false;
            }
            if (root.left == null && root.right == null) {
                return s + root.val == target;

            }
            int temp = s + root.val;
            return deal(root.left, temp) || deal(root.right, temp);
        }
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}